#include "CTaskConfig.h"
#include "CTaskID.h"

int CTaskConfig::Init()
{
    _astMainTaskConfigs.m_iUsedCount = MAX_MAIN_TASK_CONFIG;
    _astBranchTaskConfigs.m_iUsedCount = MAX_TASK_BRANCHS * MAX_TASK_CONFIG_PER_BRANCH;

    for (int i = 0; i < _astMainTaskConfigs.Size(); ++i)
    {
        _astMainTaskConfigs[i].Clear();
    }

    for (int i = 0; i < _astBranchTaskConfigs.Size(); ++i)
    {
        _astBranchTaskConfigs[i].Clear();
    }

    return 0;
}

int CTaskConfig::AddConfig(CTaskConfigData& stConfigData)
{
    if (!stConfigData.HasConfig())
    {
        return ERROR_TASK_ADD_EMPTY_CONFIG;
    }

    if (stConfigData.IsMainTask())
    {
        if (_astMainTaskConfigs[stConfigData._stTaskID._uiTaskID].HasConfig())
        {
            return ERROR_TASK_DUPLICATED_CONFIG;
        }
    }
    else
    {
        if (_astBranchTaskConfigs[stConfigData._stTaskID._uiBranchID * MAX_TASK_CONFIG_PER_BRANCH + stConfigData._stTaskID._uiTaskID].HasConfig())
        {
            return ERROR_TASK_DUPLICATED_CONFIG;
        }   
    }

    if (!stConfigData.IsValidBranchID(_astBranchTaskConfigs.Size() - 1)) 
    {
        return ERROR_TASK_BRANCH_ID;
    }

    if (!stConfigData.IsValidMainTaskID(_astMainTaskConfigs.Size() - 1))
    {
        return ERROR_TASK_TASK_ID;
    }

    if (stConfigData.IsMainTask())
    {
        _astMainTaskConfigs[stConfigData._stTaskID._uiTaskID] = stConfigData;
        _astMainTaskConfigs[stConfigData._stTaskID._uiTaskID]._bHasConfig = true;
    }
    else
    {
        _astBranchTaskConfigs[stConfigData._stTaskID._uiBranchID * MAX_TASK_CONFIG_PER_BRANCH + stConfigData._stTaskID._uiTaskID] = stConfigData;   
        _astBranchTaskConfigs[stConfigData._stTaskID._uiBranchID * MAX_TASK_CONFIG_PER_BRANCH + stConfigData._stTaskID._uiTaskID]._bHasConfig = true;
    }

    return 0;
}

int CTaskConfig::GetMainConfig(unsigned int uiTaskID, CTaskConfigData& stConfigData)
{
    if (uiTaskID >= (unsigned int)_astMainTaskConfigs.Size())
    {
        return ERROR_TASK_TASK_ID;
    }

    if (!_astMainTaskConfigs[uiTaskID]._bHasConfig)
    {
        return ERROR_TASK_TASK_ID;
    }

    stConfigData = _astMainTaskConfigs[uiTaskID];

    return 0;
}

int CTaskConfig::GetBranchConfig(unsigned int uiBranchID, unsigned int uiTaskID, CTaskConfigData& stConfigData)
{
    if (uiBranchID >= (unsigned int)_astBranchTaskConfigs.Size())
    {
        return ERROR_TASK_BRANCH_ID;
    }

    if (uiTaskID >= MAX_TASK_CONFIG_PER_BRANCH)
    {
        return ERROR_TASK_TASK_ID;
    }

    if (!_astBranchTaskConfigs[uiBranchID * MAX_TASK_CONFIG_PER_BRANCH + uiTaskID]._bHasConfig)
    {
        return ERROR_TASK_TASK_ID; 
    }

    stConfigData = _astBranchTaskConfigs[uiBranchID * MAX_TASK_CONFIG_PER_BRANCH + uiTaskID];
    return 0;
}

int CTaskConfig::GetConfig(CTaskID& stTaskID, CTaskConfigData& stConfigData)
{
    if (stTaskID.IsMainTask())
    {
        return GetMainConfig(stTaskID._uiTaskID, stConfigData);
    }
    else
    {
        return GetBranchConfig(stTaskID._uiBranchID, stTaskID._uiTaskID, stConfigData);
    }
}

